package com.onlygx.dva.common.entity.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 瞳见科技
 *
 * @author : GaoXiang
 * 时间 : 2021/12/21
 * 介绍 : 通用返回类
 */
@Data
@ApiModel("Json通用返回类")
public class RT<T> {

    private final static Integer DEFAULT_SUCCESS_CODE = 0;
    private final static Integer DEFAULT_ERROR_CODE = 599;
    private final static String DEFAULT_SUCCESS_MSG = "操作成功";

    @ApiModelProperty("请求结果代码，0：表示成功，其余状态代表失败。")
    private Integer code;

    @ApiModelProperty("请求结果描述，一般对应着 code 进行相关说明。")
    private String msg;

    @ApiModelProperty("请求结果数据，用于参数回传，可能是任意类型。")
    private T data;

    public static <Void> RT<Void> success() {
        RT<Void> r = new RT<>();
        r.code = DEFAULT_SUCCESS_CODE;
        r.msg = DEFAULT_SUCCESS_MSG;
        return r;
    }

    public static <T> RT<T> success(T data) {
        RT<T> r = new RT<>();
        r.code = DEFAULT_SUCCESS_CODE;
        r.msg = DEFAULT_SUCCESS_MSG;
        r.data = data;
        return r;
    }


    public static <T> RT<T> success(String msg,T data) {
        RT<T> r = new RT<>();
        r.code = DEFAULT_SUCCESS_CODE;
        r.msg = msg;
        r.data = data;
        return r;
    }

    public static <Void> RT<Void> success(String msg) {
        RT<Void> r = new RT<>();
        r.code = DEFAULT_SUCCESS_CODE;
        r.msg = msg;
        return r;
    }


    public static <Void> RT<Void> error(String msg) {
        RT<Void> r = new RT<>();
        r.code = DEFAULT_ERROR_CODE;
        r.msg = msg;
        r.data = null;
        return r;
    }

    public static <Void> RT<Void> error(int code, String msg) {
        RT<Void> r = new RT<>();
        r.code = code;
        r.msg = msg;
        r.data = null;
        return r;
    }

    public static <T> RT<T> error(int code, String msg, T data) {
        RT<T> r = new RT<>();
        r.code = code;
        r.msg = msg;
        r.data = data;
        return r;
    }
}
